﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DalGen.Entity;
using System.Xml;

namespace DalGen.Util
{
    public class XmlHelper
    {
        public static List<BuilderInfo> GetList()
        {

            List<BuilderInfo> entityList = new List<BuilderInfo>();

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("dal.xml");

            XmlElement element = xmlDoc.DocumentElement;

            foreach (XmlElement item in element.SelectNodes("DalProject"))
            {
                
                BuilderInfo entity = new BuilderInfo()
                {
                    Guid = item["GUID"].InnerText,
                    Alias = item["Alias"].InnerText,
                    ConnectionString = item["ConnectionString"].InnerText,
                    DataBaseName = item["DataBaseName"].InnerText,
                    DataBaseType = item["DataBaseType"].InnerText,
                    DataBaseVer = item["DataBaseVer"].InnerText,
                    EntityNameSpace = item["EntityNameSpace"].InnerText,
                    EntitySavePath = item["EntitySavePath"].InnerText,
                    IRepositoryNameSpace = item["IRepositoryNameSpace"].InnerText,
                    IRepositorySavePath = item["IRepositorySavePath"].InnerText,
                    IServiceNameSpace = item["IServiceNameSpace"].InnerText,
                    IServiceSavePath = item["IServiceSavePath"].InnerText,
                    MapXmlSavePath = item["MapXmlSavePath"].InnerText,
                    Pwd = item["Pwd"].InnerText,
                    RepositoryNameSpace = item["RepositoryNameSpace"].InnerText,
                    RepositorySavePath = item["RepositorySavePath"].InnerText,
                    Server = item["Server"].InnerText,
                    ServiceNameSpace = item["ServiceNameSpace"].InnerText,
                    ServiceSavePath = item["ServiceSavePath"].InnerText,
                    UID = item["UID"].InnerText,
                };
                entityList.Add(entity);
            }


            return entityList;
        }

        public static BuilderInfo GetEntity(string guid)
        {
            BuilderInfo entity = new BuilderInfo();

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("dal.xml");

            XmlElement element = xmlDoc.DocumentElement;

            foreach (XmlElement item in element.SelectNodes("DalProject"))
            {
                if (item["GUID"].InnerText == guid)
                {
                    entity = new BuilderInfo()
                    {
                        Guid = item["GUID"].InnerText,
                        Alias = item["Alias"].InnerText,
                        ConnectionString = item["ConnectionString"].InnerText,
                        DataBaseName = item["DataBaseName"].InnerText,
                        DataBaseType = item["DataBaseType"].InnerText,
                        DataBaseVer = item["DataBaseVer"].InnerText,
                        ProjectNameSpace = item["ProjectNameSpace"].InnerText,
                        ProjectSavePath = item["ProjectSavePath"].InnerText,
                        EntityNameSpace = item["EntityNameSpace"].InnerText,
                        EntitySavePath = item["EntitySavePath"].InnerText,
                        ServiceModelNameSpace = item["ServiceModelNameSpace"].InnerText,
                        ServiceModelSavePath = item["ServiceModelSavePath"].InnerText,
                        IRepositoryNameSpace = item["IRepositoryNameSpace"].InnerText,
                        IRepositorySavePath = item["IRepositorySavePath"].InnerText,
                        IServiceNameSpace = item["IServiceNameSpace"].InnerText,
                        IServiceSavePath = item["IServiceSavePath"].InnerText,
                        MapXmlSavePath = item["MapXmlSavePath"].InnerText,
                        Pwd = item["Pwd"].InnerText,
                        RepositoryNameSpace = item["RepositoryNameSpace"].InnerText,
                        RepositorySavePath = item["RepositorySavePath"].InnerText,
                        Server = item["Server"].InnerText,
                        ServiceNameSpace = item["ServiceNameSpace"].InnerText,
                        ServiceSavePath = item["ServiceSavePath"].InnerText,
                        UID = item["UID"].InnerText,
                    };
                    break;
                    
                }

              
            }

            return entity;

        }

        public static void AddDalInfo(BuilderInfo entity)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("dal.xml");

            XmlElement root = xmlDoc.DocumentElement;

            XmlElement elemRoot = xmlDoc.CreateElement("DalProject");
            root.AppendChild(elemRoot);

            AddXmlElement(xmlDoc, elemRoot, "GUID",entity.Guid);
            AddXmlElement(xmlDoc, elemRoot, "Alias", entity.Alias);
            AddXmlElement(xmlDoc, elemRoot, "Server", entity.Server);
            AddXmlElement(xmlDoc, elemRoot, "DataBaseType", entity.DataBaseType);
            AddXmlElement(xmlDoc, elemRoot, "DataBaseVer", entity.DataBaseVer);
            AddXmlElement(xmlDoc, elemRoot, "UID", entity.UID);
            AddXmlElement(xmlDoc, elemRoot, "Pwd", entity.Pwd);
            AddXmlElement(xmlDoc, elemRoot, "DataBaseName", entity.DataBaseName);
            AddXmlElement(xmlDoc, elemRoot, "ConnectionString", entity.ConnectionString);
            AddXmlElement(xmlDoc, elemRoot, "ProjectSavePath", entity.ProjectSavePath);
            AddXmlElement(xmlDoc, elemRoot, "EntitySavePath", entity.EntitySavePath);
            AddXmlElement(xmlDoc, elemRoot, "MapXmlSavePath", entity.MapXmlSavePath);
            AddXmlElement(xmlDoc, elemRoot, "IRepositorySavePath", entity.IRepositorySavePath);
            AddXmlElement(xmlDoc, elemRoot, "RepositorySavePath", entity.RepositorySavePath);
            AddXmlElement(xmlDoc, elemRoot, "IServiceSavePath", entity.IServiceSavePath);
            AddXmlElement(xmlDoc, elemRoot, "ServiceSavePath", entity.ServiceSavePath);
            AddXmlElement(xmlDoc, elemRoot, "ServiceModelSavePath", entity.ServiceModelSavePath);
            AddXmlElement(xmlDoc, elemRoot, "ProjectNameSpace", entity.ProjectNameSpace);
            AddXmlElement(xmlDoc, elemRoot, "EntityNameSpace", entity.EntityNameSpace);
            AddXmlElement(xmlDoc, elemRoot, "IRepositoryNameSpace", entity.IRepositoryNameSpace);
            AddXmlElement(xmlDoc, elemRoot, "RepositoryNameSpace", entity.RepositoryNameSpace);
            AddXmlElement(xmlDoc, elemRoot, "IServiceNameSpace", entity.IServiceNameSpace);
            AddXmlElement(xmlDoc, elemRoot, "ServiceNameSpace", entity.ServiceNameSpace);
            AddXmlElement(xmlDoc, elemRoot, "ServiceModelNameSpace", entity.ServiceModelNameSpace);
            xmlDoc.Save("dal.xml");

        }

        public static void Remove(string guid)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("dal.xml");

            XmlElement element = xmlDoc.DocumentElement;

            foreach (XmlElement item in element.SelectNodes("DalProject"))
            {
                if (item["GUID"].InnerText == guid)
                {
                    
                    item.RemoveAll();
                    item.ParentNode.RemoveChild(item);
                    break;
                }

            }
            xmlDoc.Save("dal.xml");


        }

        public static void Update(BuilderInfo entity)
        {

            Remove(entity.Guid);
            AddDalInfo(entity);
        }

        private static void AddXmlElement(XmlDocument xmldoc, XmlElement elemRoot, string elementName, string value)
        {
            XmlElement elem = xmldoc.CreateElement(elementName);
            elem.InnerText = value;
            elemRoot.AppendChild(elem);
            
        } 

    }
}
